package com.sense.lib.webview;


import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.webkit.JavascriptInterface;

import com.sense.lib.webview.callback.CallBackFunction;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * <br> ClassName:   GlobalJsBridge.java
 * <br> Description:  全局Js调用Native方法交互类
 * <br> Author:      lionszhang
 * <br> Date:        2022/1/15
 */
public class GlobalJsBridge {
    private CallBackFunction callBackFunction;
    private String TAG = "GlobalJsBridge";
    private Handler handler=new Handler(Looper.getMainLooper());
    public void setCallBackFunction(CallBackFunction callBackFunction) {
        this.callBackFunction = callBackFunction;
    }

    @JavascriptInterface
    public void SWITCH_TAB(final String data) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                Log.e(TAG,"SWITCH_TAB>>data="+data);
                try {
                    JSONObject jsonObject= new JSONObject(data);
                    jsonObject.put("funName","SWITCH_TAB");
                    if (callBackFunction != null) {
                        callBackFunction.onCallBack(jsonObject.toString());
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });


    }
    /**获取设备信息，请求接口需要*/
    @JavascriptInterface
    public void CLIENT_COMMPARAMS() {
        handler.post(new Runnable() {
            @Override
            public void run() {
                Log.e(TAG,"CLIENT_COMMPARAMS>>");
                try {
                    JSONObject jsonObject= new JSONObject();
                    jsonObject.put("funName","CLIENT_COMMPARAMS");
                    if (callBackFunction != null) {
                        callBackFunction.onCallBack(jsonObject.toString());
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @JavascriptInterface
    public void STAT_EVENT(final String data) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                Log.e(TAG,"STAT_EVENT>> " + data);
                try {
                    JSONObject jsonObject= new JSONObject(data);
                    jsonObject.put("funName","STAT_EVENT");
                    if (callBackFunction != null) {
                        callBackFunction.onCallBack(jsonObject.toString());
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**返回页面*/
    @JavascriptInterface
    public void GO_BACK(final String data) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                Log.e(TAG,"GO_BACK>>data="+data);
                try {
                    JSONObject jsonObject= new JSONObject(data);
                    jsonObject.put("funName","GO_BACK");
                    if (callBackFunction != null) {
                        callBackFunction.onCallBack(jsonObject.toString());
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    /**返回首页*/
    @JavascriptInterface
    public void GO_INDEX(final String data) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                Log.e(TAG,"GO_INDEX>>data="+data);
                try {
                    JSONObject jsonObject= new JSONObject(data);
                    jsonObject.put("funName","GO_INDEX");
                    if (callBackFunction != null) {
                        callBackFunction.onCallBack(jsonObject.toString());
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    /**去登录*/
    @JavascriptInterface
    public void LOGIN(final String data) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                Log.e(TAG,"LOGIN>>data="+data);
                try {
                    JSONObject jsonObject=new JSONObject(data);
                    jsonObject.put("funName","LOGIN");
                    if (callBackFunction != null) {
                        callBackFunction.onCallBack(jsonObject.toString());
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

    }
    /**登出*/
    @JavascriptInterface
    public void LOGOUT(final String data) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                Log.e(TAG,"LOGOUT>>data="+data);
                try {
                    JSONObject jsonObject=new JSONObject(data);
                    jsonObject.put("funName","LOGOUT");
                    if (callBackFunction != null) {
                        callBackFunction.onCallBack(jsonObject.toString());
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    /**上报adjust*/
    @JavascriptInterface
    public void LOG_EVENT(final String data) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                Log.e(TAG,"LOG_EVENT>>data="+data);
                try {
                    JSONObject jsonObject=new JSONObject(data);
                    jsonObject.put("funName","LOG_EVENT");
                    if (callBackFunction != null) {
                        callBackFunction.onCallBack(jsonObject.toString());
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    /**显示原生下发Tab*/
    @JavascriptInterface
    public void MAIN_TAB_TOGGLE(final String data) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                Log.e(TAG,"MAIN_TAB_TOGGLE>>data="+data);
                try {
                    JSONObject jsonObject=new JSONObject(data);
                    jsonObject.put("funName","MAIN_TAB_TOGGLE");
                    if (callBackFunction != null) {
                        callBackFunction.onCallBack(jsonObject.toString());
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    /**打开对话*/
    @JavascriptInterface
    public void OPEN_CHAT(final String data) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                Log.e(TAG,"OPEN_CHAT>>data="+data);
                try {
                    JSONObject jsonObject= new JSONObject(data);
                    jsonObject.put("funName","OPEN_CHAT");
                    if (callBackFunction != null) {
                        callBackFunction.onCallBack(jsonObject.toString());
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    /**打开聊天列表*/
    @JavascriptInterface
    public void OPEN_CHAT_LIST(final String data) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                Log.e(TAG,"OPEN_CHAT_LIST>>data="+data);
                try {
                    JSONObject jsonObject= new JSONObject(data);
                    jsonObject.put("funName","OPEN_CHAT_LIST");
                    if (callBackFunction != null) {
                        callBackFunction.onCallBack(jsonObject.toString());
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    /**打开下级webview*/
    @JavascriptInterface
    public void OPEN_WEBVIEW(final String data) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                Log.e(TAG,"OPEN_WEBVIEW>>data="+data);
                try {
                    JSONObject jsonObject= new JSONObject(data);
                    jsonObject.put("funName","OPEN_WEBVIEW");
                    if (callBackFunction != null) {
                        callBackFunction.onCallBack(jsonObject.toString());
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    /**打开下级webview*/
    @JavascriptInterface
    public void OPEN_MOBILE_BROWSER(final String data) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                Log.e(TAG,"OPEN_MOBILE_BROWSER>>data="+data);
                try {
                    JSONObject jsonObject= new JSONObject(data);
                    jsonObject.put("funName","OPEN_MOBILE_BROWSER");
                    if (callBackFunction != null) {
                        callBackFunction.onCallBack(jsonObject.toString());
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    /**快捷消息*/
    @JavascriptInterface
    public void QUICK_MESSAGE(final String data) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                Log.e(TAG,"QUICK_MESSAGE>>data="+data);
                try {
                    JSONObject jsonObject= new JSONObject(data);
                    jsonObject.put("funName","QUICK_MESSAGE");
                    if (callBackFunction != null) {
                        callBackFunction.onCallBack(jsonObject.toString());
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    /**充值*/
    @JavascriptInterface
    public void RECHARGE_SUBMIT(final String data) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                try {
                    Log.e(TAG,"RECHARGE_SUBMIT>>data="+data);
                    JSONObject jsonObject= new JSONObject(data);
                    jsonObject.put("funName","RECHARGE_SUBMIT");
                    if (callBackFunction != null) {
                        callBackFunction.onCallBack(jsonObject.toString());
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    /**第三方綁定*/
    @JavascriptInterface
    public void BIND_ACCOUNT(final String data) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                try {
                    Log.e(TAG,"BIND_ACCOUNT>>data="+data);
                    JSONObject jsonObject= new JSONObject(data);
                    jsonObject.put("funName","BIND_ACCOUNT");
                    if (callBackFunction != null) {
                        callBackFunction.onCallBack(jsonObject.toString());
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    /**上傳音頻文件*/
    @JavascriptInterface
    public void UPLOAD_VOICE(final String data) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                try {
                    Log.e(TAG,"UPLOAD_VOICE>>data="+data);
                    JSONObject jsonObject= new JSONObject(data);
                    jsonObject.put("funName","UPLOAD_VOICE");
                    if (callBackFunction != null) {
                        callBackFunction.onCallBack(jsonObject.toString());
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    /**錄音開始*/
    @JavascriptInterface
    public void RECORD_VOICE_START(final String data) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                try {
                    Log.e(TAG,"RECORD_VOICE_START>>data="+data);
                    JSONObject jsonObject= new JSONObject(data);
                    jsonObject.put("funName","RECORD_VOICE_START");
                    if (callBackFunction != null) {
                        callBackFunction.onCallBack(jsonObject.toString());
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    /**錄音開始*/
    @JavascriptInterface
    public void RECORD_VOICE_END(final String data) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                try {
                    Log.e(TAG,"RECORD_VOICE_END>>data="+data);
                    JSONObject jsonObject= new JSONObject(data);
                    jsonObject.put("funName","RECORD_VOICE_END");
                    if (callBackFunction != null) {
                        callBackFunction.onCallBack(jsonObject.toString());
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    /**上傳錄音結果*/
    @JavascriptInterface
    public void UPLOAD_VOICE_RESULT(final String data) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                try {
                    Log.e(TAG,"RECORD_VOICE_END>>data="+data);
                    JSONObject jsonObject= new JSONObject(data);
                    jsonObject.put("funName","UPLOAD_VOICE_RESULT");
                    if (callBackFunction != null) {
                        callBackFunction.onCallBack(jsonObject.toString());
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
